package gw.com.android.ui.news;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.presenter.http.FlashRequest;
import gw.com.android.ui.BaseActivity;
import www.com.library.app.PushMsgTabFragment;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.view.XRecyclerView;

/**
 * 快讯Fragment
 *
 * @author jett
 * @since 2017-11-22.
 */
public class FlashFragment extends PushMsgTabFragment implements View.OnClickListener {

    private LinearLayout llEmpty;
    private FlashAdapter mAdapter;
    private FlashRequest request = new FlashRequest();
    private XRecyclerView mRcvContent;

    @Override
    protected int getLayoutId() {
        return R.layout.view_home_flash;
    }

    @Override
    protected void initLayoutView() {
        llEmpty = mRootView.findViewById(R.id.flash_empty);
        mRcvContent = mRootView.findViewById(R.id.flash_list);
        mRcvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRcvContent.setPullRefreshEnabled(false);
        mAdapter = new FlashAdapter((BaseActivity) getActivity());
        mRcvContent.setAdapter(mAdapter);
        mRcvContent.hideFooter();
        mRcvContent.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                requestNews();
            }

            @Override
            public void onLoadMore() {
                nextPage();
            }
        });
    }

    @Override
    protected void initViewData() {
        List<FlashRequest.FlashData> list = request.fetchLocal();
        if (list != null && !list.isEmpty()) {
            refreshUi(list);
        }
        mRcvContent.setRefreshing(true);
    }

    @Override
    public void onClick(View v) {
    }

    private void refreshUi(List<FlashRequest.FlashData> list) {
        if (mAdapter == null) {
            return;
        }
        if (list != null && !list.isEmpty()) {
            mAdapter.updateData(list);
        }
        int itemCount = mAdapter.getItemCount();
        if (itemCount <= 0) {
            llEmpty.setVisibility(View.VISIBLE);
            mRcvContent.setVisibility(View.GONE);
        } else {
            llEmpty.setVisibility(View.GONE);
            mRcvContent.setVisibility(View.VISIBLE);
            mRcvContent.setNoMore(!request.hasNextPage());
        }
    }

    private void appendUi(List<FlashRequest.FlashData> list) {
        if (mAdapter == null || list == null || list.isEmpty()) {
            return;
        }
        mAdapter.appendData(list);
    }

    private void requestNews() {
        request.call(new ReqCallBack<List<FlashRequest.FlashData>>() {
            @Override
            public void onReqSuccess(List<FlashRequest.FlashData> result) {
                mRcvContent.refreshComplete();
                refreshUi(result);
                mRcvContent.setPullRefreshEnabled(false);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                mRcvContent.refreshComplete();
                refreshUi(null);
                mRcvContent.setPullRefreshEnabled(false);
            }
        });
    }

    private void nextPage() {
        request.nextPage(new ReqCallBack<List<FlashRequest.FlashData>>() {
            @Override
            public void onReqSuccess(List<FlashRequest.FlashData> result) {
                mRcvContent.loadMoreComplete();
                mRcvContent.setNoMore(!request.hasNextPage());
                appendUi(result);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                mRcvContent.loadMoreComplete();
            }
        });
    }

    @Override
    public void onRefresh() {
        mRcvContent.setPullRefreshEnabled(true);
        mRcvContent.smoothScrollToPosition(0);
        mRcvContent.setRefreshing(true);
        requestNews();
    }
}
